package edu.cmu.cs600.hw2;

/**
 * ComputeCheckDigit.java
 * Homework #2 of course 15600, finished by songluo on 9/9/2011
 * The program is given command line input consisting of the first 11 digits of a 12-digit UPC number and compute the check digit for a 12-digit UPC number. 
 * @author songluo
 *
 */
public class ComputeCheckDigit {

	/**
	 * @param inputDigit the first eleven digit of the bar code
	 * @return lastDigit the last digit of the bar code
	 */
	static int compute(String inputDigit){
		int lastDigit;
		
		lastDigit = 0;
		for(int i=0; i< inputDigit.length();i++){
			if((i % 2) == 0){
				lastDigit += (3 * Integer.parseInt(Character.toString(inputDigit.charAt(i))));
			}else{
				lastDigit += Integer.parseInt(Character.toString(inputDigit.charAt(i)));
			}
		}
		lastDigit = ( 10 - (lastDigit % 10) ) % 10;
		
		return lastDigit;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String inputStr = args[0];
		int lastDigit = compute(inputStr);
		System.out.println("The input string is : " + inputStr);
		System.out.println("The last digit is : " + lastDigit);
	}
}
